/*
 * Created on 7 juil. 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package fr.jade.service.fm.probe.ping;

import java.net.InetAddress;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.objectweb.fractal.api.factory.InstantiationException;

import fr.jade.base.JadeBaseObject;
import fr.jade.fraclite.InitializationContext;
import fr.jade.fraclite.api.Controller;
import fr.jade.fraclite.api.control.GenericAttributeController;
import fr.jade.util.JadeException;

/**
 * @author ssicard
 */
public class BasicPingController extends JadeBaseObject implements PingController, Controller {
  
  GenericAttributeController weaveableGAC;
	Executor pongExecutor;
	PongTask pong;
  
	public void initFcController(InitializationContext ic) throws InstantiationException {
	  weaveableGAC = (GenericAttributeController) ic.getInterface("generic-attribute-controller");
	  pongExecutor = Executors.newSingleThreadExecutor();
	  try {
      pong = new PongTask();
    } catch (JadeException e) {
      throw new InstantiationException("Can't create PongTask: "+e.toString());
    }
	}

  public int getPort() throws Exception {
    return pong.getPort();
  }

  public void start() throws Exception {
    log("Start Pong on "+getHostName()+":"+getPort());
    pongExecutor.execute(pong);
  }

  public void stop() throws Exception {
    pong.halt();
  }

  public String getHostName() throws Exception {
    return InetAddress.getLocalHost().getHostName();
  }
	
}
